#!/usr/bin/perl
use LWP::Simple;

$WIKIPEDIA_ARTICLE='http://en.wikipedia.org/w/index.php?title=Wikipedia:Article_Incubator/Google_Technology_User_Groups&action=edit';
$GTUG_DIRECTORY="http://www.gtugs.org/directory.jsp";
$GTUG_CHAPTER_PAGE="http://www.gtugs.org/chapter.jsp?id=";

local $| = 1;

$ua = new LWP::UserAgent;
$ua->agent("Mozilla/8.0");

# load wikipedia article as wikitext format (using edit url to get wiki tags)
print STDERR "fetching wikipedia article ";
$req = new HTTP::Request 'GET' => $WIKIPEDIA_ARTICLE;
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if ($res->is_success) {
   $wikipedia=$res->content;
} else {
   die "Error: " . $res->status_line . "\n";
}

while ($wikipedia =~ /^\|(.*?)\|\|\[(.*?)\]\|\|(.*?)\|\|(.*?)\|\|(.*?)\|\|(.*?)\|\|(.*?)$/gm) {
  my ($where,$chapter,$founded,$founders,$organizers,$status,$events)=($1,$2,$3,$4,$5,$6,$7);
  my ($chapterUrl, $chapterName)=split ' ',$chapter,2;
  $where =~ /\s*(.*?)\s*\/\s*{{Flag\|\s*(.*?)\s*}}/;
  my ($continent, $country)=($1,$2);
  $chaptersFromWikipedia{$chapterUrl}=([$chapterName, $country, $founders, $status, $events, $continent, $founded, $organizers]);
  #print "$chapterUrl / $chapterName / $country, $continent / $founded / $founders / $organizers / $status / $events\n";
}

print STDERR "\nGot ".keys (%chaptersFromWikipedia)." chapters from Wikipedia\n";

$directory=get($GTUG_DIRECTORY);
my @new=();
my @changed=();

print STDERR "Checking for new and changed chapters\n";
my $count=0;
while ($directory =~ /.*\/chapter.jsp\?id=(\d+)">/gm) {
    my $id=$1;
    my $chappage=$GTUG_CHAPTER_PAGE.$id;
    my ($organizers, $chapName, $country, $events, $status) = getChapterInfo($id);

    if (exists $chaptersFromWikipedia{$chappage}) {  # article exists on wikipedia, let's check its values
      my ($WikiChapterName, $WikiCountry, $WikiFounders, $WikiStatus, 
	$WikiEvents, $continent, $founded, $WikiOrganizers)=@{$chaptersFromWikipedia{$chappage}};
      my $changed="";
      $changed.="chaptername," unless $WikiChapterName eq $chapName;
      $changed.="status," unless $WikiStatus eq $status;
      $changed.="events," unless $WikiEvents eq $events;
      # for now, wikipedia organizers are almost always blank, and organizers are listed as founders
      $changed.="founders," unless $WikiFounders eq $organizers;
      if ($changed ne "") {
	chop $changed;
	push @changed, "[$changed]: |$continent / {{Flag| $country}}||[$chappage $chapName]||$founded||$organizers||$WikiOrganizers||$status||$events";
      }
    } else {
	push @new, "|FILLCONTINENTHERE / {{Flag| $country}}||[$chappage $chapName]|| ||$organizers||$organizers||$status||$events";
    }
    print STDERR ".";
    $count++;
}

print STDERR "\nGot $count chapters from GTUG directory. ".scalar(@new)." are new, and ".scalar(@changed)." changed\n";
print "\n------ NEW:\n";
foreach $n (@new) {
  print $n."\n";
}

print "\n------ CHANGED:\n";
foreach $c (@changed) {
  print $c."\n";
}


sub getChapterInfo() {
	my $id, $chappage, $chaptercontent, $chapName, $country, $organizer;
	$id=$_[0];
	$chappage=$GTUG_CHAPTER_PAGE.$id;
	my $req = new HTTP::Request 'GET' => $chappage;
	$req->header('Accept' => 'text/html');
	my $res = $ua->request($req);
	if ($res->is_success) {
	   $chaptercontent=$res->content;
	} else {
	   die "Error: " . $res->status_line . "\n";
	}
	if ($chaptercontent =~ /<div id="title">.*?<h2>(.*?)<\/h2>.*?<h5>(.*?)<\/h5>.*?<\/div>/ms) {
		$chapName=$1; $country=$2;
		$country =~ s/.*,\s*(.*?)\s*/\1/ms;
		$country =~ s/[\n\s]*/ /ms;
		$country =~ s/^[\n\s]*(.*?)[\n\s]*$/\1/m;
		$chapName =~ s/^[\n\s]*(.*?)[\n\s]*$/\1/m;
	}
	$organizer="";
	while ($chaptercontent =~ /<li><a href="mailto:.*?">(.*?)<\/a><\/li>/mg) {
		$organizer.="&lt;br />" if $organizer ne "";
		$organizer.=$1;
	}
	if ($chaptercontent =~ /<div><strong>Number of events:<\/strong> (\d+)<\/div>/) {
		$events=$1;
	}
	if ($chaptercontent =~ /<div><strong>Status:<\/strong> ([^ ]+)<\/div>/) {
		$status=$1;
	}
	return ($organizer, $chapName, $country, $events, $status);
}
